package com.yxy.web.controller.system;

import java.security.SecureRandom;
import java.util.Base64;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yxy.common.annotation.Log;
import com.yxy.common.core.controller.BaseController;;
import com.yxy.common.core.domain.AjaxResult;
import com.yxy.common.enums.BusinessType;
import com.yxy.system.domain.ApiConfig;
import com.yxy.system.service.IApiConfigService;
import com.yxy.common.utils.poi.ExcelUtil;
import com.yxy.common.core.page.TableDataInfo;

/**
 * 接口配置Controller
 * 
 * @author 402844673@qq.com
 * @date 2025-09-02
 */
@RestController
@RequestMapping("/system/api")
public class ApiConfigController extends BaseController
{
    @Autowired
    private IApiConfigService apiConfigService;

    /**
     * 查询接口配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:api:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApiConfig apiConfig)
    {
        startPage();
        List<ApiConfig> list = apiConfigService.selectApiConfigList(apiConfig);
        return getDataTable(list);
    }

    /**
     * 导出接口配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:api:export')")
    @Log(title = "接口配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApiConfig apiConfig)
    {
        List<ApiConfig> list = apiConfigService.selectApiConfigList(apiConfig);
        ExcelUtil<ApiConfig> util = new ExcelUtil<ApiConfig>(ApiConfig.class);
        util.exportExcel(response, list, "接口配置数据");
    }

    /**
     * 获取接口配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:api:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(apiConfigService.selectApiConfigById(id));
    }

    /**
     * 新增接口配置
     */
    @PreAuthorize("@ss.hasPermi('system:api:add')")
    @Log(title = "接口配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApiConfig apiConfig)
    {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[32];
        random.nextBytes(bytes);
        apiConfig.setPassword(Base64.getUrlEncoder().withoutPadding().encodeToString(bytes));
        return toAjax(apiConfigService.insertApiConfig(apiConfig));
    }

    /**
     * 修改接口配置
     */
    @PreAuthorize("@ss.hasPermi('system:api:edit')")
    @Log(title = "接口配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApiConfig apiConfig)
    {
        return toAjax(apiConfigService.updateApiConfig(apiConfig));
    }

    /**
     * 删除接口配置
     */
    @PreAuthorize("@ss.hasPermi('system:api:remove')")
    @Log(title = "接口配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(apiConfigService.deleteApiConfigByIds(ids));
    }
}
